<template>
  <div>
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :md="4">
          <p>搜索学员</p>
          <el-input v-model="customerQO.NameOrPhoneNumber" placeholder="请输入学员姓名或联系方式" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :md="16" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>

    <div v-if="isSuper" class="gl_mt_15 quick_date">
      <el-radio-group v-model="customerQO.FollowUpStatusCode" class="gl_mr_10" size="small" @change="getPageList()">
        <el-radio-button :label="null">全部</el-radio-button>
        <el-radio-button label="WaitFollowingUp">待跟进</el-radio-button>
      </el-radio-group>
      <el-button icon="el-icon-circle-plus-outline" type="primary" size="small" @click="clickAdd">添加客户</el-button>
    </div>

    <el-table v-loading="tableLoading" :data="tableData" class="gl_table_noBorder gl_mt_15" size="mini" style="width: 100%;">
      <el-table-column label="序号" type="index" width="46" />
      <el-table-column label="学员姓名" prop="name" width="80" />
      <el-table-column label="联系方式" prop="phoneNumber" width="100" />
      <el-table-column label="意向度" prop="intentionDegreeType.name" width="60" align="center" />
      <el-table-column label="来源渠道" prop="customerSource.name" min-width="140">
        <template slot-scope="{row}">
          <span v-if="row.customerSource.sourceGroup">{{ row.customerSource.sourceGroup.name + '-' }}</span>
          <span>{{ row.customerSource.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" :min-width="135" align="center">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="当前跟进人" prop="followUpEmployee.name" width="84" />
      <el-table-column label="跟进状态" prop="followUpStatus.name" width="72" />
      <el-table-column label="上次跟进人" prop="lastFollowUpEmployee.name" width="84" />
      <el-table-column label="最后跟进时间" min-width="135" align="center">
        <template slot-scope="{row}">
          <span v-if="row.followUpDate">
            {{ row.followUpDate | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="标签" prop="customerTag.name" :min-width="100" />
      <el-table-column label="学员性别" prop="gender.name" align="center" width="72" />
      <!--<el-table-column label="出生日期" width="85" align="center">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd') }}
          </span>
        </template>
      </el-table-column>-->
      <el-table-column label="微信" prop="wxAccount" align="center" width="100" />
      <el-table-column label="学员地址" prop="address" align="center" :min-width="140" />
      <el-table-column :label="$t('table.actions')" :width="isSuper ? 180 : 70">
        <template slot-scope="{ row }">
          <el-button :disabled="row.followUpStatusCode !== 'WaitFollowingUp'" size="mini" type="text" @click="clickReceive(row.id)">领取</el-button>
          <el-button v-if="isSuper" :disabled="row.followUpStatusCode === 'WaitFollowingUp'" size="mini" type="text" @click="clickRecovery(row.id)">回收</el-button>
          <el-button v-if="isSuper" size="mini" type="text" @click="clickDetail(row.id)">查看详情</el-button>
          <el-button v-if="row.followUpStatusCode === 'WaitFollowingUp' && isSuper" size="mini" type="text" style="color:#FF3434" @click="clickDelete(row)">删除</el-button>
          <el-button v-if="row.followUpStatusCode !== 'WaitFollowingUp' && isSuper" size="mini" :disabled="true" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="gl_pt_15" style="text-align: right;">
      <el-pagination
        :current-page="customerQO.Page"
        :page-size="customerQO.MaxResultCount"
        :total="total"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>

    <add-client ref="addClient" :has-follow="true" :title="title" :is-check="isCheck" @addCustomer="addCustomer" />
  </div>
</template>

<script>
import addClient from '../addClient.vue';
import { CopyData } from '@/lib';
import { mapState, mapActions } from 'vuex';
import {
  Api_GET_ClubCustomerQO, // 分页获取客户信息
  Api_GET_ClubCustomerIdQO // 根据Id获取客户信息
} from '@/lib/form/hall/swaggerForm';
import {
  api_DELETE_ClubCustomer, // 删除客户信息
  api_POST_ClubCustomerReceiverCustomerId, // 领取客户
  api_POST_ClubCustomerRecycleCustomerId, // 回收到客户池
  api_POST_ClubCustomer, // 创建客户信息
  api_GET_ClubCustomer, // 分页获取客户信息
  api_GET_ClubCustomerId // 根据Id获取客户信息
} from '@/extend/api/hallswaggerApi/app';
export default {
  components: { addClient },
  data() {
    return {
      title: '添加客户',
      isCheck: false,
      customerQO: new Api_GET_ClubCustomerQO(),
      tableLoading: false,
      total: 0,
      tableData: [],
      isSuper: false
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      info: state => state.user.info
    })
  },
  mounted() {
    this.getDict();
    const groups = this.info.permissions;
    this.isSuper = groups.findIndex(item => item === 'Club.CRM') !== -1;
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    // 获取客户列表
    getPageList(page) {
      this.customerQO.Page = page || 1;
      this.customerQO.SkipCount = (this.customerQO.Page - 1) * this.customerQO.MaxResultCount;
      this.customerQO.IncludeGender = true;
      this.customerQO.IncludeCustomerSource = true;
      this.customerQO.IncludeCustomerTag = true;
      this.customerQO.IncludeFollowUpStatus = true;
      this.customerQO.IncludeFollowUpEmployee = true;
      this.customerQO.IncludeLastFollowUpEmployee = true;
      this.customerQO.IncludeIntentionDegreeType = true;
      this.customerQO.IsCustomerPool = true;
      this.customerQO.FollowUpStatusCode = this.isSuper ? this.customerQO.FollowUpStatusCode : 'WaitFollowingUp';
      this.tableLoading = true;
      api_GET_ClubCustomer(this.customerQO).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickAdd() {
      this.title = '添加客户';
      this.isCheck = false;
      this.$refs.addClient.dialogVisible = true;
      this.$refs.addClient.customerInput.genderCode = 'M';
    },
    addCustomer(data) {
      console.log(data);
      this.$refs.addClient.btnLoading = true;
      api_POST_ClubCustomer(data).then(res => {
        this.$refs.addClient.btnLoading = false;
        this.$message({ type: 'success', message: '添加成功' });
        this.$refs.addClient.cancelDialog();
        this.getPageList();
      }, error => {
        this.$refs.addClient.btnLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickRecovery(id) {
      this.tableLoading = true;
      api_POST_ClubCustomerRecycleCustomerId(id).then(res => {
        this.tableLoading = false;
        this.$message({ type: 'success', message: '回收成功' });
        this.getPageList();
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickReceive(id) {
      this.tableLoading = true;
      api_POST_ClubCustomerReceiverCustomerId(id).then(res => {
        this.tableLoading = false;
        this.$message({ type: 'success', message: '领取成功' });
        this.getPageList();
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickDelete(item) {
      const that = this;
      that.$confirm(`您确定要删除${item.name}吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.tableLoading = true;
          api_DELETE_ClubCustomer({ 'customerId': item.id }).then(res => {
            this.tableLoading = false;
            that.$message({ type: 'success', message: '删除成功' });
            that.getPageList();
          }, error => {
            this.tableLoading = false;
            const _msg = error.error.message || error.message;
            that.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    clickDetail(id) {
      this.title = '查看客户信息';
      this.isCheck = true;
      this.$refs.addClient.dialogVisible = true;
      const qo = new Api_GET_ClubCustomerIdQO();
      qo.IncludeIntentionDegreeType = true;
      qo.IncludeGender = true;
      qo.IncludeIdCardType = true;
      qo.IncludeContactType = true;
      qo.IncludeCustomerSource = true;
      qo.IncludeCustomerTag = true;
      qo.IncludeFollowUpStatus = true;
      qo.IncludeFollowUpEmployee = true;
      qo.IsCustomerPool = true;
      this.$refs.addClient.loading = true;
      api_GET_ClubCustomerId(id, qo).then(res => {
        CopyData(res, this.$refs.addClient.customerInput);
        this.$refs.addClient.loading = false;
        this.$refs.addClient.customerInput.idCardTypeCode = res.idCardType && res.idCardType.name;
        this.$refs.addClient.customerInput.contactTypeCode = res.contactType && res.contactType.name;
        this.$refs.addClient.customerInput.genderCode = res.gender && res.gender.name;
        this.$refs.addClient.customerInput.customerSourceId = res.customerSource && res.customerSource.name;
        this.$refs.addClient.customerInput.customerTagId = res.customerTag && res.customerTag.name;
        this.$refs.addClient.customerInput.intentionDegreeTypeCode = res.intentionDegreeType && res.intentionDegreeType.name;
        this.$refs.addClient.customerInput.followUpEmployeeId = res.followUpEmployee && res.followUpEmployee.name;
        console.log(res);
      }, error => {
        this.$refs.addClient.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickReset() {
      this.customerQO = new Api_GET_ClubCustomerQO();
      this.getPageList();
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.customerQO.MaxResultCount = MaxResultCount;
      this.getPageList();
    },
    pageIndexChange(currentPage) {
      this.getPageList(currentPage);
    }
  }
};
</script>
<style lang="scss" scoped>
.quick_date {
  font-size: 12px;
  line-height: 32px;
}
</style>
